package com.longdaji.decoration.ui.wear;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.bean.CaptainInfoBean;
import com.longdaji.decoration.data.bean.GoodsAccountBean;
import com.longdaji.decoration.ui.main.MainActivity;
import com.longdaji.decoration.utils.ImageUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by longtao on 2017/12/19.
 */

public class WearActivity extends BaseActivity implements WearContract.View {

    private static final String TAG = WearActivity.class.getSimpleName();
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_goods_num)
    TextView mTvGoodsNum;
    @BindView(R.id.llyt_no_captain)
    LinearLayout mLlytNoCaptain;
    @BindView(R.id.iv_image)
    ImageView mIvImage;
    @BindView(R.id.recycler_captain)
    RecyclerView mRecyclerViewCaptain;
    @Inject
    WearPresenter mPresenter;
    private CaptainInfoAdapter adapter;

    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, WearActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "PayActivity onCreate");
        setContentView(R.layout.activity_wear);
        setUnBinder(ButterKnife.bind(this));
        initView();
    }

    @Override
    protected void onDestroy() {
        mPresenter.detachView();
        super.onDestroy();
    }

    private void initView() {
        mPresenter.setView(this);
        mTvTitle.setText("预约试戴");
        if (getIntent() != null) {
            //getData(getIntent().<CartBean.Cart>getParcelableArrayListExtra("cart_list"));
            int num = getIntent().getIntExtra("num", 0);
            String image = getIntent().getStringExtra("image");
            mTvGoodsNum.setText(num + "件商品");
            //GlideUtils.loadImage(this, image, mIvImage, null, R.drawable.no_banner, R.drawable.no_banner);
            ImageUtil.loadImage(this, image,mIvImage);
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

        mRecyclerViewCaptain.setLayoutManager(layoutManager);
        adapter = new CaptainInfoAdapter(this, new ArrayList<CaptainInfoBean>());
        //mRecyclerViewBrand.addItemDecoration(new GridSpacingItemDecoration(2, 10, false));
        mRecyclerViewCaptain.setAdapter(adapter);
        getData();
    }

    private void getData() {
        //mPresenter.getAccountData(1, cartList);
        mPresenter.queryCaptainInfo(Account.getInstance().getUserid());
    }

    @OnClick({R.id.iv_back, R.id.btn_captain, R.id.btn_home})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_captain:
                break;
            case R.id.btn_home:
                startActivity(MainActivity.getStartIntent(this));
                break;
            default:
                break;
        }
    }

    @Override
    public void showGoodsAndReceiverData(GoodsAccountBean goodsAccountBean) {
        int num = 0;
        for (GoodsAccountBean.CartBalance.CartGoods cartGoods : goodsAccountBean.getCartBalance().getCartBalanceGoodsList()) {
            num += cartGoods.getGoodsSkuNum();
        }
        mTvGoodsNum.setText(num + "件商品");

    }

    @Override
    public void showNoCaptain() {
        mRecyclerViewCaptain.setVisibility(View.GONE);
        mLlytNoCaptain.setVisibility(View.VISIBLE);

    }

    @Override
    public void showCaptainInfo(List<CaptainInfoBean> captainInfoList) {
        mLlytNoCaptain.setVisibility(View.GONE);
        mRecyclerViewCaptain.setVisibility(View.VISIBLE);
        adapter.setData(captainInfoList);

    }


}
